package com.laiketui.admin.mch.controller;

import com.laiketui.admin.mch.api.GoodsService;
import com.laiketui.common.api.PublicBomService;
import com.laiketui.common.api.PublicGoodsService;
import com.laiketui.common.api.PubliceService;
import com.laiketui.core.domain.Result;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.core.lktconst.GloabConst;
import com.laiketui.domain.bom.BomModel;
import com.laiketui.domain.vo.BomAddVo;
import com.laiketui.domain.vo.MainVo;
import com.laiketui.domain.vo.admin.DefaultViewVo;
import com.laiketui.domain.vo.goods.BrandClassVo;
import com.laiketui.domain.vo.mch.UploadMerchandiseVo;
import com.laiketui.root.annotation.HttpApiMethod;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 商品管理
 *
 * @author Trick
 * @date 2021/5/31 15:55
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("/admin/mch/goods/")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private PublicBomService bomService;

    @Autowired
    private PublicGoodsService publicGoodsService;

    @ApiOperation("商品列表")
    @PostMapping("/index")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.Index")
    public Result index(DefaultViewVo vo, HttpServletResponse response) {
        try {
            return Result.success(goodsService.index(vo, response));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("代售可选商品列表")
    @PostMapping("/consignmentPro")
    @HttpApiMethod(apiKey = "admin.mch.goods.consignmentPro")
    public Result consignmentPro(DefaultViewVo vo) {
        try {
            return Result.success(goodsService.consignmentPro(vo));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("获取店铺商品配置")
    @PostMapping("/getCommoditySetup")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.GetCommoditySetup")
    public Result getCommoditySetup(MainVo vo) {
        try {
            return Result.success(goodsService.getCommoditySetup(vo));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @Autowired
    private PubliceService publiceService;

    @ApiOperation("商品支持的活动类型")
    @PostMapping("/getGoodsActive")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.GetGoodsActive")
    public Result getGoodsActive(MainVo vo) {
        try {
            return Result.success(publiceService.getGoodsActive(vo.getStoreId()));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("商品标签列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键id", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "name", value = "商品名称", dataType = "string", paramType = "form"),
    })
    @PostMapping("/getGoodsLabel")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.GetGoodsLabel")
    public Result getGoodsLabel(MainVo vo, String name, Integer id) {
        try {
            return Result.success(goodsService.getGoodsLabel(vo, name, id));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation(("获取分类列表"))
    @PostMapping("/getClassList")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.GetClassList")
    public Result getClassList(MainVo vo){
        try {
            return Result.success(publicGoodsService.getMchCLassList(vo));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }



    @ApiOperation("获取类别信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classId", value = "类型id", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "brandId", value = "品牌id", dataType = "int", paramType = "form"),
    })
    @PostMapping("/getClass")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.GetClass")
    public Result getClass(MainVo vo, Integer classId, Integer brandId,Integer mchId) {
        try {
            return Result.success(goodsService.getClass(vo, classId, brandId,mchId));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("获取商品属性(规格)名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "attributesName", value = "规格名称", dataType = "string", paramType = "form")
    })
    @PostMapping("/getAttributeName")
    @HttpApiMethod(apiKey = "admin.mch.goods.getAttributeName")
    public Result getAttributeName(MainVo vo, String attributesName) {
        try {
            Map<String, Object> result = goodsService.getAttributeName(vo, attributesName);
            return Result.success(result);
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("获取商品属性(规格)值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "attributesIds", value = "规格id,多个用','", required = true, dataType = "string", paramType = "form")
    })
    @PostMapping("/getAttributeValue")
    @HttpApiMethod(apiKey = "admin.mch.goods.getAttributeValue")
    public Result getAttributeValue(MainVo vo, String attributesIds) {
        try {
            Map<String, Object> result = goodsService.getAttributeValue(vo, attributesIds);
            return Result.success(result);
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }


    @ApiOperation("获取商品规格列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品id", required = true, dataType = "int", paramType = "form")
    })
    @PostMapping("/getAttrByGoodsId")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.GetAttrByGoodsId")
    public Result getAttrByGoodsId(MainVo vo, Integer goodsId) {
        try {
            return Result.success(goodsService.getAttrByGoodsId(vo, goodsId));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("获取商品规格和包件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品id", required = true, dataType = "int", paramType = "form")
    })
    @PostMapping("/getAttrAndBomByGoodsId")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.GetAttrAndBomByGoodsId")
    public Result getAttrAndBomByGoodsId(MainVo vo, Integer goodsId) {
        try {
            return Result.success(goodsService.getAttrAndBomByGoodsId(vo, goodsId));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("获取(发布商品)商品页面数据")
    @PostMapping("/getAddPage")
    @HttpApiMethod(apiKey = "admin.mch.goods.getAddPage")
    public Result getAddPage(MainVo vo) {
        try {
            return Result.success(goodsService.getAddPage(vo));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("上传商品")
    @PostMapping("/addGoods")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.AddGoods")
    public Result addGoods(UploadMerchandiseVo vo) {
        try {
            return Result.success(goodsService.addGoods(vo));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("添加包件")
    @PostMapping("addBom")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.AddBom")
    public Result addBom(BomAddVo vo){
        try{
            bomService.addBom(vo);
            return Result.success();
        }catch (LaiKeAPIException e){
            return Result.error(e.getCode(), e.getMessage());
        }
    }


    @ApiOperation("上架自选商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsIds", value = "自选商品id集合','", required = true, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "yunFeiId", value = "运费模板id','", required = true, dataType = "int", paramType = "form"),
    })
    @PostMapping("/addZxGoods")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.AddZxGoods")
    public Result addZxGoods(MainVo vo, String goodsIds, Integer yunFeiId) {
        try {
            goodsService.addZxGoods(vo, goodsIds, yunFeiId);
            return Result.success(GloabConst.ManaValue.MANA_VALUE_SUCCESS);
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("编辑商品属性上架状态")
    @PostMapping("/editConfigureStatus")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.editConfigureStatus")
    public Result editConfigureStatus(int configureId, Integer status) {
        try {
            boolean flag = publicGoodsService.editConfigureStatus(configureId, status);
            if(flag == true){
                return Result.success("修改成功");
            }else{
                return Result.error("修改失败");
            }

        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("编辑商品序号")
    @PostMapping("/editSort")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.editSort")
    public Result editSort(MainVo vo, Integer id, Integer sort) {
        try {
            goodsService.editSort(vo, id, sort);
            return Result.success(GloabConst.ManaValue.MANA_VALUE_SUCCESS);
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("根据id获取商品信息(编辑商品)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品id", required = true, dataType = "int", paramType = "form"),
    })
    @PostMapping("/getGoodsInfoById")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.GetGoodsInfoById")
    public Result getGoodsInfoById(MainVo vo, int goodsId) {
        try {
            return Result.success(goodsService.getGoodsInfoById(vo, goodsId, false));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }


    @ApiOperation("上下架商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsIds", value = "商品id集", dataType = "string", paramType = "form")
    })
    @PostMapping("/upperAndLowerShelves")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.UpperAndLowerShelves")
    public Result upperAndLowerShelves(MainVo vo, String goodsIds, Integer status) {
        try {
            boolean result = goodsService.upperAndLowerShelves(vo, goodsIds, status);
            return Result.success(result);
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }


    @ApiOperation("添加库存页面数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsIds", value = "商品id集", dataType = "int", required = true, paramType = "form")
    })
    @PostMapping("/stockPage")
    @HttpApiMethod(apiKey = "admin.mch.goods.stockPage")
    public Result stockPage(MainVo vo, int goodsIds) {
        try {
            return Result.success(goodsService.stockPage(vo, goodsIds));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }


    @ApiOperation("增加库存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stock", value = "[{id:1,addNum:1,pid:123},{id:2,addNum:1,pid:123}...] json数据", dataType = "int", required = true, paramType = "form")
    })
    @PostMapping("/addStock")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.AddStock")
    public Result addStock(MainVo vo, String stock) {
        try {
            return Result.success(goodsService.addStock(vo, stock));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }


    @ApiOperation("删除商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsIds", value = "商品id 集", dataType = "string", required = true, paramType = "form")
    })
    @PostMapping("/delGoodsById")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.DelGoodsById")
    public Result delGoodsById(MainVo vo, String goodsIds) {
        try {
            goodsService.delGoodsById(vo, goodsIds);
            return Result.success(GloabConst.ManaValue.MANA_VALUE_SUCCESS);
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }


    @ApiOperation("获取商品审核列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classId", value = "商品类别id", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "brandId", value = "商品品牌id", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "goodsName", value = "商品名称", dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "examineStatus", value = "1=审核中 3=审核未通过  4=待审核", dataType = "int", paramType = "form"),
    })
    @PostMapping("/getGoodsExamineInfo")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.GetGoodsExamineInfo")
    public Result getGoodsExamineInfo(MainVo vo, Integer classId, Integer brandId, Integer examineStatus, String goodsName, Integer goodsId) {
        try {
            return Result.success(goodsService.getGoodsExamineInfo(vo, classId, brandId, examineStatus, goodsName, goodsId));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation("提交审核/撤销审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pIds", value = "商品id 集", required = true, dataType = "string", paramType = "form")
    })
    @PostMapping("/submitAudit")
    @HttpApiMethod(apiKey = "mch.Mch.Goods.SubmitAudit")
    public Result submitAudit(MainVo vo, String pIds) {
        try {
            return Result.success(goodsService.submitAudit(vo, pIds));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }


    @ApiOperation("上传图片")
    @PostMapping("/uploadImgs")
    @HttpApiMethod(apiKey = "admin.mch.goods.uploadImgs")
    public Result uploadImgs(MainVo vo, MultipartFile image) {
        try {
            return Result.success(goodsService.uploadImgs(vo, image));
        } catch (LaiKeAPIException e) {
            return Result.error(e.getCode(), e.getMessage());
        }
    }

}
